﻿namespace GGFramework.Core.Timer
{
    public class FrameTimer : TimerBase
    {
        public int FrameCount { get; private set; }
        public int RemainingFrames { get; private set; }

        public FrameTimer() : base(0) { }

        public void Init(int frames)
        {
            FrameCount = frames;
            RemainingFrames = frames;
        }

        public override void Update(float deltaTime)
        {
            if (IsPaused || IsCompleted) return;

            RemainingFrames--;
            UpdateCallback?.Invoke(RemainingFrames);

            if (RemainingFrames <= 0)
            {
                Complete();
                IsCompleted = true;
            }
        }

        public override void Reset()
        {
            base.Reset();
            RemainingFrames = FrameCount;
        }

        public override bool IsExpired => RemainingFrames <= 0;
        
        public override void Cancel()
        {
            IsCompleted = true;
        }
    }
}